const Downloader = require("../index").Downloader;
const path = require("path");
const process = require("process");
const uuid = require("uuid");
let d = new Downloader(
  path.join(
    path.resolve(process.cwd()), "tmp/db"
  )
);
debugger
d.getDbTaskCount().then(o => {
  debugger
    ((add) => {
      if (!add) return;
      d.addTask('https://sukebei.nyaa.si/download/2630440.torrent',
        path.join(__dirname, "../tmp", uuid.v1()), {
          来源: "测试旧版本webtorrent"
        }).then(o => {
        debugger
      });
    })(true);
    d.on("task-duplicate", (info) => {
      debugger
    });

    d.on("task-speed", (info) => {
      // console.log(info.reason);//将速度文字化的结果
      /*形如|[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s]
      [down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s]
      ,[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],
      [down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s]
      [down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],
      [down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],
      [down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],
      [down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],
      [down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],
      [down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:0 B/s,up:0 B/s],
      [down:0 B/s,up:0 B/s],[down:3.13 KB/s,up:0 B/s],[down:3.13 KB/s,up:0 B/s],[down:3.13 KB/s,up:0 B/s],
      [down:3.13 KB/s,up:0 B/s],[down:0 B/s,up:0 B/s],[down:3.13 KB/s,up:0 B/s],[down:3.13 KB/s,up:0 B/s],
      [down:6.33 KB/s,up:6.26 KB/s],[down:6.33 KB/s,up:6.26 KB/s],[down:9.53 KB/s,up:6.26 KB/s]
      ,[down:9.6 KB/s,up:6.26 KB/s],[down:9.6 KB/s,up:6.26 KB/s],[down:12.8 KB/s,up:0 B/s],
      [down:12.8 KB/s,up:0 B/s],[down:19.2 KB/s,up:0 B/s],[down:16 KB/s,up:0 B/s],[down:12.8 KB/s,up:0 B/s],
      [down:9.6 KB/s,up:0 B/s],[down:6.4 KB/s,up:0 B/s],[down:19.2 KB/s,up:0 B/s],[down:25.6 KB/s,up:0 B/s],
      [down:38.4 KB/s,up:0 B/s],[down:41.6 KB/s,up:0 B/s],[down:48 KB/s,up:0 B/s],[down:41.6 KB/s,up:0 B/s],
      [down:41.6 KB/s,up:0 B/s],[down:44.8 KB/s,up:0 B/s],[down:54.4 KB/s,up:0 B/s],[down:64 KB/s,up:0 B/s],
      [down:80 KB/s,up:0 B/s],[down:89.6 KB/s,up:0 B/s],[down:99.2 KB/s,up:0 B/s],[down:99.2 KB/s,up:0 B/s],
      [down:105.6 KB/s,up:0 B/s],[down:99.2 KB/s,up:0 B/s],[down:99.2 KB/s,up:0 B/s],[down:96 KB/s,up:0 B/s],
      [down:86.4 KB/s,up:0 B/s],[down:83.2 KB/s,up:0 B/s],[down:96 KB/s,up:0 B/s],[down:92.8 KB/s,up:19.2 KB/s],
      [down:96 KB/s,up:22.4 KB/s],[down:99.2 KB/s,up:25.6 KB/s],[down:92.8 KB/s,up:32 KB/s],[down:92.8 KB/s,up:16 KB/s],
      [down:99.2 KB/s,up:19.2 KB/s],[down:99.2 KB/s,up:35.2 KB/s],[down:108.8 KB/s,up:48 KB/s],[down:108.8 KB/s,up:54.4 KB/s],
      [down:92.8 KB/s,up:54.4 KB/s],[down:83.2 KB/s,up:54.4 KB/s],[down:76.8 KB/s,up:41.6 KB/s],[down:70.4 KB/s,up:38.4 KB/s],
      [down:76.8 KB/s,up:32 KB/s],[down:86.4 KB/s,up:28.8 KB/s],[down:86.4 KB/s,up:25.6 KB/s],[down:80 KB/s,up:19.2 KB/s]
      ,[down:76.8 KB/s,up:6.4 KB/s],[down:73.6 KB/s,up:6.4 KB/s],[down:64 KB/s,up:9.6 KB/s],[down:73.6 KB/s,up:6.4 KB/s],
      [down:89.6 KB/s,up:3.2 KB/s],[down:89.6 KB/s,up:6.4 KB/s],[down:99.2 KB/s,up:9.6 KB/s],[down:105.6 KB/s,up:6.4 KB/s],
      [down:105.6 KB/s,up:9.6 KB/s],[down:102.4 KB/s,up:19.2 KB/s],[down:112 KB/s,up:25.6 KB/s],[down:105.6 KB/s,up:28.8 KB/s],[down:96 KB/s,up:32 KB/s],[down:89.6 KB/s,up:28.8 KB/s],[down:86.4 KB/s,up:16 KB/s],[down:83.2 KB/s,up:3.2 KB/s]
      ,[down:89.6 KB/s,up:0 B/s],[down:92.8 KB/s,up:19.2 KB/s],
      [down:99.2 KB/s,up:28.8 KB/s],[down:105.6 KB/s,up:35.2 KB/s],[down:112 KB/s,up:60.8 KB/s]|*/
    })

    d.on("task-fail", (info) => {
      debugger
      /**info
       * 
       * 实在没有种子的时候
       * Object {task: Task, code: "NO-SEEDER", reason: "NO METADATA after 2 minutes"}
       * Task=Task {_schema: Object, _id: "1a36xBb8udLSQWVi", torrentId: "magnet:?xt=urn:btih:e98a262cd0228e41f8243dacec33a7…", …}
       */
    });

    d.on("task-done", (info) => {
      // debugger
      let torrentId = info['task']['torrentId'];
      d.emit("task-delete",torrentId)
      debugger
      /*

  Object {task: Task {_schema: Object, _id: "4jVvsymgw8eUlJk0", 
  torrentId: "https://dl.dmhy.org/2018/11/06/074fa025b6bcbdc9f74…", …},
   code: "OK", reason: "OK"}
  */
    });
})